import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr={1,2,4,3,7,5,9,0,6,8};

        for(int i=0; i<arr.length-1; ++i) {
            boolean flg = true;
            for(int j=0; j<arr.length-1-i; ++j) {

                if(arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;

                    flg= false;
                }
            }
            if(flg) {
                break;
            }
        }

        System.out.println(Arrays.toString(arr));

        int[] arr2={1,2,4,3,7,5,9,0,6,8};
        bubbleSort2(arr2);
        System.out.println(Arrays.toString(arr2));

    }

    //第二种写法
    public static void bubbleSort2(int[] arr) {
        if(arr==null || arr.length<2) {
            return;
        }
        for(int end = arr.length-1; end >0; --end) {
            boolean flg=true;
            for(int i=0; i<end; ++i) {
                if(arr[i] > arr[i+1]) {
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = temp;
                    flg=false;
                }
            }
            if(flg) {
                break;
            }
        }
    }

}
